package com.hunter.sign.common.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.hunter.common.util.MD5Util;
import com.hunter.common.util.MapConver;
import com.hunter.common.util.ParamMd5Sig;
import com.hunter.sign.common.constant.SignConstant;

public class ParamProcess{
	
	/**
	 * app接口参数验证
	 * @param o
	 * @param cls
	 * @return
	 */
	public static boolean paramValid(Object o, Class<?> cls){
		Map<String, String> map = MapConver.getMapvalueFromObject(o, cls);
		if(StringUtils.isBlank(map.get("flagtime"))){
			return false;
		}
		long flagtime = Long.valueOf(map.get("flagtime"));
		if(Math.abs(flagtime-System.currentTimeMillis())>2*60*1000){//请求时间和服务器时间超过2min
			return false;
		}
		String sig = map.get("sig");
		map.remove("sig");
		return ParamMd5Sig.generateSig(map, SignConstant.SIG_KEY).equalsIgnoreCase(sig);
	}
	
	public static void main(String[] args) {
//		Map<String, String> map = new HashMap<String,String>();
//		map.put("flagtime", "1500432946120");
//		String sig = ParamMd5Sig.generateSig(map, SignConstant.SIG_KEY);
//		System.out.println(sig);
		String sec = SignConstant.SIG_KEY;
		System.out.println(MD5Util.md5("fieldFill={\"date\":\"2017.7.24\",\"witness\":\"dsl\",\"assurer\":\"dsl\"}&flagtime=1500886604715&userContractId=7sigkey654321"));
	}
}
